package libcore.base;

import android.text.TextUtils;
import com.google.gson.annotations.SerializedName;

public class Result<T> {
  @SerializedName("errorCode")
  public int code;
  @SerializedName("errorMsg")
  public String msg;
  public T data;

  public boolean isSuccess() {
    return code == 0;
  }

  public boolean hasData() {
    return isSuccess() && data != null;
  }

  public static String getErrorMsg(Result result) {
    return getErrorMsg(result, "数据错误");
  }

  public static String getErrorMsg(Result result, String error) {
    if (result != null) {
      if (!result.hasData() && !TextUtils.isEmpty(result.msg)) {
        return result.msg;
      }
    }
    return error;
  }
}
